package com.sparrow.mvc.ui;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;

import com.sparrow.utils.StringUtil;

/**
 * value:name,value2:name2
 * 
 * @author zhanglizhi
 * @date 2012-3-8 下午09:15:59
 * @version 1.0
 */
public class JDropDownList extends JWebControl {
	private static final long serialVersionUID = 8678128692032102460L;
	// 只读属性
	private String readonly;
	// 不可用
	private String disabled;
	// 多选
	private String multiple;
	// key value
	private String keyValues;
	// 选中的key
	private String selectKey;

	public String getSelectKey() {
		return selectKey;
	}

	public void setSelectKey(String selectKey) {
		this.selectKey = selectKey;
	}

	@SuppressWarnings("unchecked")
	public String getItemList() {
		// 后台可以设置 OPTION
		Map<String, String> listItem = (LinkedHashMap<String, String>) super.pageContext
				.getRequest().getAttribute(super.getId() + ".options");
		// 如果为空则获取前台的设置值0:女,1:男,2:保密
		if (listItem == null) {
			listItem = new LinkedHashMap<String, String>();
			String[] options;
			String[] option = new String[2];
			if (!StringUtil.isNullOrEmpty(this.getKeyValues())) {
				options = this.getKeyValues().split(",");
				for (int i = 0; i < options.length; i++) {
					option = options[i].split(":");
					listItem.put(option[0], option[1]);
				}
			}
		}
		Set<String> keySet = listItem.keySet();
		Iterator<String> it = keySet.iterator();
		String listKey = "";
		Object selectValue = super.pageContext.getRequest().getAttribute(
				this.getCtrlName());
		if (StringUtil.isNullOrEmpty(selectValue)) {
			selectValue = super.pageContext.getRequest().getParameter(
					this.getCtrlName());
		}
		if (StringUtil.isNullOrEmpty(selectValue)) {
			selectValue = this.getSelectKey();
		}
		StringBuffer options = new StringBuffer();
		while (it.hasNext()) {
			listKey = it.next().toString();
			if (listKey.equals(selectValue)) {
				options
						.append(String
								.format(
										"\n<option selected=\"selected\" value=\"%1$s\">%2$s</option>",
										listKey, listItem.get(listKey)));
			} else {
				options.append(String.format(
						"\n<option value=\"%1$s\">%2$s</option>", listKey,
						listItem.get(listKey)));
			}
		}
		return options.toString();
	}

	public String getKeyValues() {
		return keyValues;
	}

	public void setKeyValues(String keyValues) {
		this.keyValues = keyValues;
	}

	public String getReadonly() {
		Object requestReadonly = this.pageContext.getRequest().getAttribute(
				this.getId() + ".readonly");
		if (requestReadonly != null) {
			if (requestReadonly.toString().toLowerCase().trim() != "false")
				return " readonly= \"readonly\" ";
			else
				return "";
		} else {
			if (this.readonly != null && this.readonly.trim() != "false") {
				return " readonly= \"readonly\" ";
			} else {
				return "";
			}
		}
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getDisabled() {
		Object requestDisabled = this.pageContext.getRequest().getAttribute(
				this.getId() + ".disabled");
		if (requestDisabled != null) {
			if (requestDisabled.toString().toLowerCase().trim() != "false")
				return " disabled= \"disabled\" ";
			else
				return "";
		} else {
			if (this.disabled != null && this.disabled.trim() != "false") {
				return " disabled= \"disabled\" ";
			} else {
				return "";
			}
		}
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getMultiple() {
		if (this.multiple != null) {
			return "multiple=\"" + multiple + "\"";
		} else {
			return "";
		}
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	@Override
	public int doEndTag() throws JspException {
		if (this.getVisible() != "false") {
			try {
				this.pageContext.getOut().print("\n</select>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return super.doEndTag();
	}

	@SuppressWarnings("static-access")
	@Override
	public int doStartTag() throws JspException {
		int returnValue = super.SKIP_BODY;
		if (this.getVisible().toLowerCase() != "false") {
			StringBuffer writeHTML = new StringBuffer();
			writeHTML.append(String.format("<%1$s id=\"%2$s\"", "select", this
					.getId()));
			writeHTML.append(this.getName());
			writeHTML.append(this.getCssClass());
			writeHTML.append(this.getCssText());
			writeHTML.append(this.getTitle());
			writeHTML.append(this.getReadonly());
			writeHTML.append(this.getDisabled());
			writeHTML.append(this.getMultiple());
			writeHTML.append(">");
			writeHTML.append(this.getItemList());
			try {
				this.pageContext.getOut().print(writeHTML.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return returnValue;
	}
}
